/***************************************************************************
 *   Copyright (C) 2008, 2009 by MJ   *
 *   mj_dv@web.de   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

//steamserverutil is a program to manage the steam game servers from hldsupdatetool.bin
//v0.5, last changes: 13.12.09


#include <iostream> //for cout and cin
#include <fstream> //for ifstream and ofstream
#include <stdlib.h> //for system()	
#include <string.h> //for strcpy, strcat and strcmp

//for gettext
#include <libintl.h>
#include <locale.h>

#define _(STRING) gettext(STRING)

#include "steamserverutil.h"
#include "screen.h"
#include "conf_server.h"


using namespace std;


//main menu
int main(int argc, char *argv[])
{
	//gettext
	setlocale (LC_ALL, "");
	bindtextdomain ("steamserverutil","/usr/share/locale");
	textdomain("steamserverutil");
	
	system_check(system("clear;clear;clear")); //for a better look ;)
	
	
	char choice;

	cout << "steamserverutil " << _version << ", steamserverutil.googlecode.com\n";	
	
	home_dir = getenv("HOME");
	home_dir += "/";

	
	ifstream test_file;
	param = home_dir + ".steamserverutil/workdir";
	test_file.open(param.c_str());
	if(test_file.is_open())
	{
		getline(test_file, workdir);
	}
	else
	{
		workdir = home_dir + ".steamserverutil/";	//workdir is set to home dir = default
		
		string ex;
		cout << _("Creating directory...\n");
		ex = "mkdir " + workdir;
		system_check(system(ex.c_str()));
		
		
		ofstream out;
		param = workdir + "workdir";
		out.open(param.c_str());
		out << workdir; //write down new workdir
		
		out.close();
		
		param = workdir + "workdirs";
		out.open(param.c_str(), ios_base::app);
		out << workdir; //add cur workdir to workdirs
		out.close();
				
	}
	
	test_file.close();
	
	
	//set workdir path
	param = workdir + "/hldsupdatetool.bin";
	
	
	test_file.open(param.c_str());

	if(test_file.is_open())
	{
		cout << _("found hldsupdatetool.bin\n");
	}
	else
	{
		string ex;
		cout << _("Downloading hldsupdatetool.bin with wget...\n\n");
		ex = "cd " + workdir + " && wget http://www.steampowered.com/download/hldsupdatetool.bin && chmod +x hldsupdatetool.bin";
		
		system_check(system(ex.c_str()));
	}
	test_file.close();

	
	//set workdir path
	param = workdir + "/steam";
	
	test_file.open(param.c_str());
	
	if( !test_file.is_open() )
	{
		cout << _("\n\nFirst you have to accept the license agreement, show now?(y|n) ");
		cin >> choice;

		cout << "\n\n";
	
		if (choice == 'y')
		{
			param = "cd " + workdir + " && ./hldsupdatetool.bin";
			system_check(system(param.c_str()));
		}
		else
		{
			cout << _("Aborting...\n");
			return 2;
		}	
			
	}
	
	

	test_file.close();
	
	//set workdir path
	param = workdir + "/steam";
	
	test_file.open(param.c_str());
	


	if( test_file.is_open())
	{
		cout << _("found steam\n");
		cout << _("Current workspace is \"") << workdir << "\"";
		test_file.close();
		
		
		//now checking parameters
		if(argc == 3)
		{
			if(!strcmp(argv[1], "--exec-fav"))
			{
				int temp;
				sscanf(argv[2], "%d", &temp);
				favorites(temp);
				return 0;
			}
			
			if(!strcmp(argv[1], "--workspace"))
			{
				workdir = argv[2];
				return 0;
			}
		}
		
		
		
		bool exit=false;

		
		while(!exit)
		{
			unsigned int menu;
			cout << _("\n\n\t**main menu**\n");
			cout << _("1. Install or update game server\n");
			cout << _("2. Start game server\n");
			cout << _("3. Currently running servers with \"screen\"\n");
			cout << _("4. Favorites\n");
			cout << _("5. Configure\n");
			cout << _("6. Update Steam Linux Client (recommend for first use)\n");
			cout << _("7. Delete servers\n");
			cout << _("8. About\n");
			cout << _("9. Exit\n");
			cout << " : ";
			cin >> menu;
			
			cin.ignore();

			switch (menu)
			{
				case 1: 	install();
				break;
				case 2: 	start();
				break;
				case 3:		screen_test();
				break;
				case 4: 	favorites();
				break;
				case 5:		config_server();
				break;
				case 6:		update_client();
				break;
				case 7: 	delete_server();
				break;
				case 8:		about();
				break;
				case 9: 	exit=true;
				break;
				default:	cout << _("Try again ;)");
				break;
			}

		}
	}
	else
	{
		never_happen();
		cout << _("\nUnknown Error!\n");
		return 1;
	}


	return 0;

}